<template>
  <div>
    <avue-crud :option="option"
               :data="data"
               ref="crud"
               :page="page"
               v-model="form"
               :permission="permissionList"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   v-if="permission.dict_delete"
                   plain
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </div>
</template>

<script>
    //国家字典 country
    import {delRowCountry, editRowCountry, queryDictCountry, saveRowCountry} from "@/api/system/countryDict";
    //城市字典 city
    import {delRowCity, editRowCity, queryDictCity, saveRowCity} from "@/api/system/cityDict";
    //单位字典 company
    import {delRowCompany, editRowCompany, queryDictCompany, saveRowCompany} from "@/api/system/companyDict";
    //通用字典
    import {delRowGener, saveRowGener, editRowGener, queryDictGener} from "@/api/system/generalDict";

    import {mapGetters} from "vuex";

    export default {
        name: "CommonDict",
        props: ['name', 'option'],
        data() {
            return {
                form: {},
                selectionList: [],
                query: {},
                page: {
                    pageSize: 10,
                    currentPage: 1,
                    total: 0
                },
                formslot: true,
                data: []
            }
        },
        computed: {
            ...mapGetters(["permission"]),
            permissionList() {
                return {
                    addBtn: true,
                    viewBtn: true,
                    delBtn: true,
                    editBtn: true
                };
            },
            ids() {
                let ids = [];
                this.selectionList.forEach(ele => {
                    ids.push(ele.id);
                });
                return ids.join(",");
            }
        },
        methods: {
            searchList(params) {//查询
                switch (this.name) {
                    case "companyDict":
                        return queryDictCompany(params)
                        break;
                    case "countryDict":
                        return queryDictCountry(params)
                        break;
                    case "cityDict":
                        return queryDictCity(params)
                        break;
                    case "generalDict":
                        return queryDictGener(params)
                        break;
                    default:
                        break
                }
            },
            editList(params) {//编辑
                switch (this.name) {
                    case "companyDict":
                        return editRowCompany(params)
                        break;
                    case "countryDict":
                        return editRowCountry(params)
                        break;
                    case "cityDict":
                        return editRowCity(params)
                        break;
                    case "generalDict":
                        return editRowGener(params)
                        break;
                    default:
                        break
                }
            },
            saveList(params) {//保存
                switch (this.name) {
                    case "companyDict":
                        return saveRowCompany(params)
                        break;
                    case "countryDict":
                        return saveRowCountry(params)
                        break;
                    case "generalDict":
                        return saveRowGener(params)
                        break;
                    case "cityDict":
                        return saveRowCity(params)
                        break;
                    default:
                        break
                }
            },
            delList(params) {//删除
                switch (this.name) {
                    case "companyDict":
                        return delRowCompany(params)
                        break;
                    case "countryDict":
                        return delRowCountry(params)
                        break;
                    case "cityDict":
                        return delRowCity(params)
                        break;
                    case "generalDict":
                        return delRowGener(params)
                        break;
                    default:
                        break
                }
            },
            rowSave(row, loading, done) {
                this.saveList(row).then((res) => {
                    loading();
                    this.onLoad();
                }, error => {
                    done();
                });
            },
            rowUpdate(row, index, loading, done) {
                this.editList(row).then(() => {
                    loading();
                    this.onLoad();
                }, error => {
                    done();
                    console.log(error);
                });
            },
            rowDel(row) {
                this.$confirm("确定将选择数据删除?", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "success"
                }).then(() => {
                    return this.delList(row.id);
                }).then(() => {
                    this.onLoad();
                });
            },
            handleDelete() {
                if (this.selectionList.length === 0) {
                    this.$message.warning("请选择至少一条数据");
                    return;
                }
                this.$confirm("确定将选择数据删除?", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "success"
                }).then(() => {
                    return this.delList(this.ids);
                }).then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!"
                    });
                    this.$refs.crud.toggleSelection();
                });
            },
            searchReset() {
                this.query = {};
                this.onLoad();
            },
            searchChange(params) {
                this.query = params;
                this.page.pageSize = 10;
                this.page.currentPage = 1;
                this.onLoad();
            },
            selectionChange(list) {
                this.selectionList = list;
            },
            onLoad() {
                let req = {}
                this.$nextTick(() => {
                    if(typeof this.page != 'boolean') {
                        req = Object.assign({}, this.query, {
                            "current": this.page.currentPage,
                            "size": this.page.pageSize
                        });
                    }
                    this.searchList(req).then(res => {
                        if (this.name == 'countryDict' || this.name == 'cityDict' || this.name == 'companyDict') {
                            this.data = res.data.data.records;
                            this.page.total = res.data.data.total
                        } else {
                            this.data = res.data.data;
                            this.$nextTick(() => {
                                this.page = {}
                            })
                            if(this.name==='generalDict'){
                                this.option.column[2].dicData = this.data;
                            }
                        }
                    }).catch(e => {
                        console.log(e)
                    })
                })
            },
            currentChange(val) {
                this.page.currentPage = val
            },
            sizeChange(val) {
                this.page.pageSize = val
            }
        }
    }
</script>

<style scoped>

</style>
